#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef struct student
{
    int id;
    char name[10];
    int age;
}stu;

void InitStu(stu* s,int id,char *name,int age)
{
    s->id = id;
    s->age = age;
    strcpy(s->name,name);
}

void PrintStu(const stu* s)
{
    printf("学生id:%d 姓名：%s 年龄：%d\n",s->id,s->name,s->age);
}

typedef struct teacher
{
    char name[10];
    int age;
    stu student[10];
    int count;
}teach;

void InitTeach(teach *t,const char *name,int age)
{
    strcpy(t->name,name);
    t->age = age;
    for(int i = 0; i < 10; i++)
    {
        InitStu(&t->student[i],0,"",0);
    }
    t->count = 0; 
}

void AddStudent(teach *t,int id,char *name,int age)
{
    if(t->count > 10)
    {
        printf("学生人数满了！\n");
        return;
    }
    InitStu(&t->student[t->count++],id,name,age);
}

void PrintTeach(const teach *t)
{
    printf("老师名字：%s 年龄：%d\n",t->name,t->age);
    for(int i = 0; i < t->count; i++)
    {
        printf("学生信息：");
        PrintStu(&t->student[i]);
    }
}

bool CheckId(stu *s1,stu *s2)
{
    if(s1->id > s2->id)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool CheckAge(stu *s1,stu *s2)
{
    if(s1->age > s2->age)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool CheckName(stu *s1,stu *s2)
{
    if(strcmp(s1->name,s2->name))
    {
        return true;
    }
    else
    {
        return false;
    }
}

void FastSort(stu *s,int start,int end,bool (*funcptr)(stu *s1,stu *s2))
{
    if(start >= end)
    return;
    int left = start;
    int right = end;
    stu temp = s[left];
    while(left < right)
    {
        while(left < right && funcptr(&s[right],&temp) == true)
        right--;
        if(left < right)
        {
            s[left] = s[right];
            left++;
        }
        while(left < right && funcptr(&s[left],&temp) == false)    
        left++;
        if(left < right)
        {
            s[right] = s[left];
            right--;
        }
        s[right] = temp;
        
    }
    FastSort(s,start,left-1,funcptr);
    FastSort(s,right+1,end,funcptr);
}

void SortStudent(teach *t,bool (*funcptr)(stu *,stu *))
{
    FastSort(t->student,0,t->count-1,funcptr);
}
int main()
{
    teach t;
    InitTeach(&t,"李老师",40);
    AddStudent(&t,3,"zhangsan",20);
    AddStudent(&t,1,"lisi",19);
    AddStudent(&t,2,"wangwu",18);
    AddStudent(&t,4,"xiaoming",21);

    SortStudent(&t,CheckAge);
    PrintTeach(&t);

    return 0;
}